package com.netty.modbus.repository;

import com.netty.modbus.dto.request.ReadBody;
import com.netty.modbus.dto.request.RequestBody;
import com.netty.modbus.dto.request.WriteBody;
import com.netty.modbus.dto.response.ReadResponseBody;
import io.netty.channel.Channel;

import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;

/**
 * description: 设备与通道关联资源类 <br>
 * @date: 2021/10/9 9:29 <br>
 * @author: lwy <br>
 * @version: 1.0 <br>
 */
public class DevChannelRepository {
    /**
     * 设备编号与通道关联map
     * */
    private Map<String, Channel> devChannel=new ConcurrentHashMap();
    /**
     * 存放读命令队列，
     * */
    private ConcurrentLinkedDeque<RequestBody> readCommand=new ConcurrentLinkedDeque<>();


    private static volatile DevChannelRepository devChannelRepository;

    private DevChannelRepository (){}
    /**
     * 单例模式获取类
     * */
    public static DevChannelRepository getInstance(){
        if(devChannelRepository==null){
            synchronized (DevChannelRepository.class){
                if(devChannelRepository==null){
                    devChannelRepository=new DevChannelRepository();
                }
            }
        }
        return devChannelRepository;
    }

    public Map<String, Channel> getDevChannel() {
        return devChannel;
    }

    /**
     * description: 放入设备编号和通道
     * version: 1.0
     * date: 2021/10/11 9:24
     * @author: lwy
     * @param devNumber 设备编号
     * @param channel 通道
     * @return void
     */
    public void setDevChannel(String devNumber,Channel channel) {
        this.devChannel.put(devNumber.toUpperCase(),channel);
    }

    public boolean hasDev(String devNumber){
        return devChannel.get(devNumber.toUpperCase())==null?false:true;
    }

    public ConcurrentLinkedDeque<RequestBody> getCommand() {
        return readCommand;
    }

    public void setCommand(RequestBody requestBody) {
        this.readCommand.offer(requestBody);
    }

}
